'use strict'

const Controller = require('egg').Controller;

class UserController extends Controller {
    async create() {
        const {ctx} = this;
        const { name, age } = ctx.request.body;
        const user = await ctx.model.User.create({ name, age });
        ctx.status = 201;
        ctx.body = user;
    }
    async index(){
        const {ctx}=this;
        ctx.body = await ctx.model.User.findAll();
    }
    async update(){
        const {ctx}=this;
        const id=ctx.params.id;
        const user= await ctx.model.User.findByPk(id);
        if(!user){
            ctx.status=404;
            return;
        }
        const {name,age}=ctx.request.body;
        await user.update({name,age});
        ctx.body=user;
    }
}

module.exports = UserController;